<?php
class EditUserForm extends Form
{
	function EditUserForm(){
		Form::Form('EditUser');
		$this->link_css('style/personal.css');
		$this->link_css('style/aznet/ui.datepicker.css');
		$this->link_js('javascript/jquery/packed/jquery.validate.js');
		$this->link_js('javascript/jquery/ui.datepicker.js');		
		$this->link_js('javascript/personal.js');	
		$this->link_css('style/manage_item.css');
		$this->link_js('javascript/jquery/jquery.jcache.js');
		$this->link_js('javascript/jquery/jquery.livequery.js');	
		$this->link_js('javascript/jquery/packed/jquery.jeditable.js');	
		
		CGlobal::$website_title = "Thay đổi thông tin cá nhân";						
	}

	function on_submit(){				
		if(!User::is_block()){
			$file_name = strtolower($_FILES['avatar_url']['name']);
			$upload_path  = AZLib::folderUpload(User::id(),'avatar');
			
			$max_upload_file_size = 2*1024*1024;
			$file_ext = AZLib::getExtension(strtolower($file_name));
			$new_path = '';
			
			if($file_name && in_array($file_ext,array('.jpg','.jpeg','.gif','.png')) && filesize($_FILES['avatar_url']['tmp_name'])< $max_upload_file_size){			
				if(AZLib::ftp_check_dir($upload_path, true, IMAGE_SERVER_NO)){				
					if(User::$current->data['avatar_url'] <> ''){
						@AZLib::ftp_image_delete_file(User::$current->data['avatar_url'],User::$current->data['img_server']);
					}
					$new_path   = $upload_path.date("YmdHis",TIME_NOW) . '_'.substr(AZLib::make_safe_name(basename($file_name,$file_ext)),0,36).$file_ext;	
					@AZLib::ftp_image_put_file($new_path,$_FILES['avatar_url']['tmp_name'],IMAGE_SERVER_NO);
				}
			}
			
			$phone_verify = User::$current->data['phone_verify'];			
			$this->update_user($new_path,$phone_verify,IMAGE_SERVER_NO);//đang làm dở
			User::getUser(User::id(),true);
			echo '<script>document.location.href="'.WEB_ROOT.AZRewrite::formatUrl('?page=personal&cmd=edit_user&action=success_edit_user').'"</script>';
		}
		else{
			$this->setFormError('',"Tài khoản của bạn hiện đang bị khóa, bạn không thể thực hiện chức năng này!");
		}
	}	
	
	function draw(){
	
		global $display;		
		if(isset($_POST['update'])){
			$this->on_submit();
		}				
		
		$user = PersonalDB::get_user_information();
		foreach($user as $key=>$value){
			if(is_string($value) and !isset($_REQUEST[$key])){
				$_REQUEST[$key] = $value;
			}
		}
		
		if($user['show_home_phone'] == 0){
			$user['show_home_phone_checked'] = '';
		}	
		else{
			$user['show_home_phone_checked'] = 'checked';
		}
		
		if($user['show_email'] == 0){
			$user['checked'] = '';
		}	
		else{
			$user['checked'] = 'checked';
		}
		
		if($user['email_alert'] == 0){
			$user['alert_email_checked'] = '';
		}	
		else{
			$user['alert_email_checked'] = 'checked';
		}
		
		if($user['birth_day']){
			$arrBirtday = explode('-',$user['birth_day']);
			$user['birth_day'] = $arrBirtday['2'].'-'.$arrBirtday['1'].'-'.$arrBirtday['0'];
		}				
								
	
		if($user['avatar_url']!=""){
			$user['avatar_url'] = '<img src="'.AZLib::getImageThumb($user['avatar_url'],100,100,0,$user['img_server']).'" />';
		}
		else{
			$user['avatar_url'] = '<img src="style/images/no_avatar_item.gif" width="94" height="94" />';
		}

		$display->add('msg',$this->showFormErrorMessages(1));
		$display->add('user',$user);
		
		$openids=array();
		
		$re=DB::query("SELECT id, openid_url FROM openid WHERE user_id=".$user['id']);
		if($re){
			while ($openid=mysql_fetch_assoc($re)){
				$openid['openid']=AZLib::strippedLink($openid['openid_url'],48).' <img src="style/images/delete.gif" title="Xoá bỏ OpenID Url này khỏi tài khoản của bạn!" onclick="return del_openid('.$openid['id'].');" />';
				
				
				$openids[$openid['id']]=$openid;
			}
		}
		$display->add('openids',$openids);					
		
		$display->add('can_edit_blast',((User::is_admin()) || !User::is_block()));
		$blast			= User::$current->data['blast'];
		$avatar_link 	= '';
		$avatar_url 	= 'style/images/no_avatar_item.gif';
			
		if(User::$current->data['avatar_url'] != ''){
			$avatar_link 	= "http://".CGlobal::$img_server[User::$current->data['img_server']].User::$current->data['avatar_url'];
			$avatar_url 	= AZLib::getImageThumb(User::$current->data['avatar_url'],80,80,0,User::$current->data['img_server']);
		}
		
		if($blast == ''){
			$blast = '...';
		}
		else{
			$blast = ' '.$blast;
			if(strpos($blast,'http://')){
				$user_blast = $blast;
				while(strpos($user_blast,'http://')){
					$tmp = substr($user_blast,strpos($user_blast,'http://'));
					
					if(strpos($tmp,' ') || strpos($tmp,'<')){
						if(strpos($tmp,' ') && strpos($tmp,'<') && (strpos($tmp,' ') > strpos($tmp,'<'))){
							$blast_url = substr($tmp,0,strpos($tmp,'<'));
						}
						else{
							$blast_url = substr($tmp,0,strpos($tmp,' '));
						}
					}
					else{
						$blast_url = $tmp;
					}
					$user_blast = str_replace($blast_url,'',$user_blast);
				}
				$new_blast_url = ' <a rel="nofollow" href="'.$blast_url.'" target="_blank">Click here</a>';
				$blast = substr($user_blast.$new_blast_url,1);
			}
		}
		
		$display->add('avatar_link',$avatar_link);
		$display->add('avatar_url',$avatar_url);
		$display->add('blast',$blast);
		
		$display->add('get_cmd',Url::get('cmd'));					
		$display->add('get_action',Url::get('action'));
		$display->add('get_page',Url::get('page'));			
		$display->add('url',Url::build_all());
		$display->add('user_name', User::user_name());			
		$display->add('user_id',User::id());			
		$TopMenu = $display->output('TopMenu',true,'Personal');
		$display->add('TopMenu',$TopMenu);			
		$display->output('edit_user',false,'Personal');				
	}	
	
	function update_user($new_path,$phone_verify,$img_server){
		if(!isset($_POST['show_email'])){
			$show_email = 0;
		}
		else{
			$show_email = 1;
		}
		if(!isset($_POST['show_home_phone'])){
			$show_home_phone = 0;
		}
		else{
			$show_home_phone = 1;
		}
		if(!isset($_POST['email_alert'])){
			$email_alert = 0;
		}
		else{
			$email_alert = 1;
		}
		$id_card = Url::get('id_card');
		if(AZLib::checkBadWord($id_card)){
			$id_card = '';
		}
		$new_blast = Url::get('blast');
		if(AZLib::checkBadWord($new_blast)){
			$new_blast = '';
		}
		$new_sign = Url::get('sign');
		if(AZLib::checkBadWord($new_sign)){
			$new_sign = '';
		}
		$full_name = Url::get('full_name');
		if(AZLib::checkBadWord($full_name)){
			$full_name = '';
		}
		$address = Url::get('address');
		if(AZLib::checkBadWord($address)){
			$address = '';
		}
		
		$home_phone = Url::get('home_phone');		
		$home_phone = preg_replace("/[^0-9]/", "", $home_phone);
		if(AZLib::is_mobile($home_phone) || substr($home_phone,0,1)!="0"){
			$home_phone = '';
		}				
		
		$fax = Url::get('fax');
		$fax = preg_replace("/[^0-9]/", "", $fax);
		
		
		$mobile_phone = Url::get('mobile_phone');						
		$mobile_phone = preg_replace("/[^0-9]/", "", $mobile_phone);
		if(!AZLib::is_mobile($mobile_phone)){
			$mobile_phone = '';
		}
		
		$website = Url::get('website');
		if(AZLib::checkBadWord($website)){
			$website = '';
		}
		$yahoo_id = Url::get('yahoo_id');
		if(AZLib::checkBadWord($yahoo_id)){
			$yahoo_id = '';
		}
		$skype_id = Url::get('skype_id');
		if(AZLib::checkBadWord($skype_id)){
			$skype_id = '';
		}
		
		$arr = explode('-',Url::get('birth_day'));
		if(is_numeric($arr['0']) && is_numeric($arr['1']) && is_numeric($arr['2'])){
			$strBirthDay = $arr['2'].'-'.$arr['1'].'-'.$arr['0'];
		}
		else{
			$strBirthDay = "";
		}
		
		if($new_path){
		
			$new_row=array	(
							'full_name'=>trim($full_name),
							'address'=>trim($address),			
							'id_card'=>$id_card,			
							'birth_day'=>$strBirthDay,
							'gender'=>intval(Url::get('gender')),
							'home_phone'=>trim($home_phone),
							'fax'=>trim($fax),
							'mobile_phone'=>trim($mobile_phone),
							'show_home_phone'=>$show_home_phone,
							'show_email'=>$show_email,
							'email_alert'=>$email_alert,
							'website'=>trim($website),
							'yahoo_id'=>trim($yahoo_id),
							'skype_id'=>$skype_id,
							'blast'=>trim($new_blast),
							'signature'=>trim($new_sign),
							'img_server'=>$img_server,
							'avatar_url'=>trim($new_path)
						);
		}
		else {
			$new_row=array	(
							'full_name'=>trim($full_name),
							'address'=>trim($address),
							'id_card'=>$id_card,			
							'birth_day'=>$strBirthDay,
							'gender'=>intval(Url::get('gender')),
							'home_phone'=>trim($home_phone),
							'show_home_phone'=>$show_home_phone,
							'fax'=>trim($fax),
							'mobile_phone'=>trim($mobile_phone),
							'show_email'=>$show_email,
							'email_alert'=>$email_alert,
							'website'=>trim($website),
							'yahoo_id'=>trim($yahoo_id),
							'skype_id'=>trim($skype_id),
							'blast'=>($new_blast),
							'signature'=>trim($new_sign)						
						);
		}
		
		PersonalDB::update_user_information($new_row);		
		
		if($new_blast){//blast không trống					
				DB::delete("feed","user_id=".User::id()." AND type = 8");				
				DB::query("INSERT INTO feed (type, user_id, act_user_id, time)
										VALUES (8, ".User::id().", ".User::id().", ".TIME_NOW.")");					
		}
		else{//Xoá blast
			DB::delete("feed","user_id=".User::id()." AND type = 8");
		}
		
	}
			
}
?>
